package com.ruitu.mall_98_2.cart;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyRefreshBaseFragment;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.view.AmountView;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.CartContract;
import com.ruitu.mall_98_2.mvp.model.CartModel;
import com.ruitu.mall_98_2.mvp.presenter.CartPresenter;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.cart.CartBean;
import com.ruitu.router_module.bean.product.ProHome;
import com.ruitu.router_module.bean.product.Product;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class CartFragment extends MyRefreshBaseFragment<CartPresenter, CartModel> implements CartContract.View, CheckSelectInterface {
    private DataAdapter adapter;
    private com.ruitu.router_module.bean.DataAdapter recommendAdapter;
    private CheckSelectInterface checkSelectInterface;//选择的接口
    private CartBean cartBeans;
    private CheckBox cb_check_all;//全选
    private TextView tv_check_all;//全选
    private TextView tv_commodity_amount;//结算商品的数量
    private RelativeLayout layout_cart_none;//购物车为空时显示的布局
    private RelativeLayout layout_error;//购物车加载错误的布局
    private LinearLayout ll_bottom;//底部结算按钮
    private TextView tv_walk;//逛逛
    private TextView tv_delete_commodity;//删除
    private TextView tv_total;//合计
    private TextView tv_agin;//重新加载
    private LRecyclerView lRecyclerView;
    private int count;
    int shopPosition;
    private AlertDialog alertDialog;
    private String id;

    private int deleteAmount;//删除数量
    private List<ProHome> globalProList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!Arad.bus.isRegistered(this)) {
            Arad.bus.register(this);
        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Window window = getActivity().getWindow();
//            window.setStatusBarColor(Color.TRANSPARENT);
////            window.
//
//        }
    }

    @Override
    protected int getInflate() {
        return R.layout.fragment_cart;
    }

    @Override
    protected void initRecyclerView(View view) {
        super.initRecyclerView(view);
        //选择接口的注册
        setCheckSelectInterface(this);
        setLoadMoreEnabled(false);
        cb_check_all = view.findViewById(R.id.cb_check_all);
        tv_check_all = view.findViewById(R.id.tv_check_all);
        tv_commodity_amount = view.findViewById(R.id.tv_commodity_amount);

        layout_error = view.findViewById(R.id.layout_error);
        ll_bottom = view.findViewById(R.id.ll_bottom);
        tv_delete_commodity = view.findViewById(R.id.tv_delete_commodity);
        tv_total = view.findViewById(R.id.tv_total);
        tv_agin = view.findViewById(R.id.tv_agin);
        cb_check_all.setOnClickListener(this);
        tv_check_all.setOnClickListener(this);
        tv_delete_commodity.setOnClickListener(this);
        tv_commodity_amount.setOnClickListener(this);
        tv_agin.setOnClickListener(this);
        if (CommonUtils.isLogin()) {
            lRecyclerView.setVisibility(View.VISIBLE);
        } else {
            lRecyclerView.setVisibility(View.GONE);
        }

        View foot_view = View.inflate(getContext(), R.layout.item_foot_view, null);
        lAdapter.addFooterView(foot_view);
        RecyclerView recyclerView = foot_view.findViewById(R.id.recyclerView);
        tv_walk = foot_view.findViewById(R.id.tv_walk);
        layout_cart_none = foot_view.findViewById(R.id.layout_cart_none);
        tv_walk.setOnClickListener(this);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        recommendAdapter = new com.ruitu.router_module.bean.DataAdapter(getContext());
        recyclerView.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Product product = new Product();
                product.setGoodId(globalProList.get(position).getGoodsId());
                Bundle bundle = new Bundle();
                bundle.putSerializable("product", product);
                try {
                    A_Rutils.startActivity(getContext(), "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected LRecyclerView getRecyclerView(View view) {
        lRecyclerView = view.findViewById(R.id.lRecyclerView);
        return lRecyclerView;
    }

    private void setCheckAll(boolean checked) {
        for (int i = 0; i < cartBeans.getCarts().size(); i++) {
            cartBeans.getCarts().get(i).setGroupChecked(checked);
            for (int j = 0; j < cartBeans.getCarts().get(i).getList().size(); j++) {
                cartBeans.getCarts().get(i).getList().get(j).setChildChecked(checked);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (tv_check_all == v) {
            cb_check_all.setChecked(!cb_check_all.isChecked());
            setCheckAll(cb_check_all.isChecked());
        }
        if (cb_check_all == v) {
            boolean checked = cb_check_all.isChecked();
            cb_check_all.setChecked(checked);
            setCheckAll(checked);
        }
        if (v == tv_walk) {
            Arad.bus.post(new EventModel(18122001));
        }
        if (v == tv_delete_commodity) {
            StringBuffer buffer = new StringBuffer();
            String s = null;
            for (int i = 0; i < cartBeans.getCarts().size(); i++) {
                for (int j = 0; j < cartBeans.getCarts().get(i).getList().size(); j++) {
                    if (cartBeans.getCarts().get(i).getList().get(j).isChildChecked()) {
                        String id = cartBeans.getCarts().get(i).getList().get(j).getId();
                        s = buffer.append(id).append(",").toString();
                    }
                }
            }
            if (!TextUtils.isEmpty(s)) {
                deleteDialog(s);
            } else {
                ToastUtils.showShort("请选择要删除的商品");
            }
        }
        if (v == tv_commodity_amount) {
            StringBuffer buffer = new StringBuffer();
            String s = null;
            boolean isHasXiajia = false;//是否选择了下架的商品(下架的不能买)
            boolean isUpdate = false;//是否选择了修改规格的商品(修改规格的不能买)
            int selectNum = 0;//记录选中的商品的个数
            for (int i = 0; i < cartBeans.getCarts().size(); i++) {
                CartBean.CartsBean group = cartBeans.getCarts().get(i);
                for (int j = 0; j < group.getList().size(); j++) {
                    CartBean.CartsBean.ListBean child = group.getList().get(j);
                    if (child.isChildChecked()) {
                        String id;
                        if (child.getModelType().equals("1")) {
                            id = child.getSkuId();
                        } else {
                            id = child.getGoodsId();
                        }

                        s = buffer.append(id).append(",").toString();

                        selectNum++;
                        if (TextUtils.equals(child.getStatus(), "0")) {//0 下架 1 在售
                            isHasXiajia = true;
                        }
                        if (!child.isUpdate()) {//false 修改了规格的
                            isUpdate = true;
                        }
                    }
                }
            }
            if (isHasXiajia) {//说明选择了下架的商品
                if (selectNum > 1) {
                    ToastUtils.showShort("部分商品已下架,暂时不能购买");
                } else {
                    ToastUtils.showShort("商品已下架,暂时不能购买");
                }
                return;
            }
            if (isUpdate) {//说明选择了修改规格的商品
                if (selectNum > 1) {
                    ToastUtils.showShort("部分商品已更新,不能购买");
                } else {
                    ToastUtils.showShort("商品已更新,不能购买");
                }
                return;
            }
            if (TextUtils.isEmpty(s)) {
                ToastUtils.showShort("请选择要结算的商品");
            } else {
                try {
                    Bundle bundle = new Bundle();
                    bundle.putString("ids", s);
                    A_Rutils.startActivity(getContext(), "com.ruitu.mall_98_2.order.CommitOrderActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        if (tv_agin == v) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                showProgress();
                getCartList();
            }
        }
    }

    @Override
    protected ProgressLayout findProgressLayout(View view) {
        return view.findViewById(R.id.progress);
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(getContext());
        return adapter;
    }

    @Override
    protected void reqFirst() {
        getCartList();
    }

    @Override
    protected void reqMore() {
    }

    @Override
    protected void reReq() {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getCartList(EventModel<ProHome> eventModel) {
        if (eventModel.getEventCode() == 18110900) {
            getCartList();
            lRecyclerView.setVisibility(View.VISIBLE);
        }
        if (eventModel.getEventCode() == 180102500) {
            adapter.setDataList(null);
            adapter.notifyDataSetChanged();
            ll_bottom.setVisibility(View.GONE);
            lRecyclerView.setVisibility(View.GONE);
        }
        if (eventModel.getEventCode() == 18122751) {
            globalProList = AppHolder.instance().getGlobalProList();
            recommendAdapter.setDataList(globalProList);
        }
    }

    public void getCartList() {
//        showProgress();
        mPresenter.getCarts(AppHolder.instance().getUser().getAccountId());
    }

    @Override
    public void setData(CartBean cartBeans) {
        this.cartBeans = cartBeans;
        refreshComplete();
        if (layout_error.getVisibility() == View.VISIBLE) {
            layout_error.setVisibility(View.GONE);
        }
//        if (lRecyclerView.getVisibility() == View.GONE) {
//            lRecyclerView.setVisibility(View.VISIBLE);
//        }
        if (isDataEmpty(cartBeans.getCarts())) {
            layout_cart_none.setVisibility(View.VISIBLE);
            ll_bottom.setVisibility(View.GONE);
//            lRecyclerView.setVisibility(View.GONE);
            tv_delete_commodity.setVisibility(View.GONE);
            notifyDataSetChanged();
        } else {
            notifyDataSetChanged();
            layout_cart_none.setVisibility(View.GONE);
            ll_bottom.setVisibility(View.VISIBLE);
            lRecyclerView.setVisibility(View.VISIBLE);
            tv_delete_commodity.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void setFaiel() {
        refreshComplete();
        layout_error.setVisibility(View.VISIBLE);
        lRecyclerView.setVisibility(View.GONE);
        ll_bottom.setVisibility(View.GONE);
    }

    @Override
    public void setDeleteSuc() {
        getCartList();
    }

    @Override
    public void updateCartNumSuc() {
        getCartList();
    }

    @Override
    public void setString(String total) {
        tv_total.setText("￥" + total);
    }

    @Override
    public void checkShop(int shopPosition, boolean isSelect) {
        CartBean.CartsBean cartsBean = cartBeans.getCarts().get(shopPosition);
        for (int i = 0; i < cartsBean.getList().size(); i++) {
            cartBeans.getCarts().get(shopPosition).getList().get(i).setChildChecked(isSelect);
        }
        notifyDataSetChanged();
    }

    @Override
    public void checkCommodity(int shopPosition, int commodity, boolean isSelect) {
        cartBeans.getCarts().get(shopPosition).getList().get(commodity).setChildChecked(isSelect);
        if (isShopCommodityAll(shopPosition)) {
            cartBeans.getCarts().get(shopPosition).setGroupChecked(true);
        } else {
            cartBeans.getCarts().get(shopPosition).setGroupChecked(false);
        }
        notifyDataSetChanged();
    }

    class DataAdapter extends ListBaseAdapter<CartBean.CartsBean> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_shop_group;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {

            final CartBean.CartsBean cartBean = mDataList.get(position);
            CheckBox ck_shop_cat_group = holder.getView(R.id.ck_shop_cat_group);
            ImageView iv_shop_img = holder.getView(R.id.iv_shop_img);
            TextView tv_shop_name = holder.getView(R.id.tv_shop_name);
            LinearLayout ll_shop = holder.getView(R.id.ll_shop);
            RecyclerView recyclerView = holder.getView(R.id.recyclerView);
            Glide.with(mContext).load(Config.BASE_URL + cartBean.getList().get(0).getCompanyLogo()).apply(Arad.opts_shop).into(iv_shop_img);
//            if (position == 0) {
//                ll_header.setVisibility(View.VISIBLE);
//            } else {
//                ll_header.setVisibility(View.GONE);
//            }
            ll_shop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        Bundle bundle = new Bundle();
                        bundle.putString("companyId", cartBean.getCompanyId());
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ShopDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
            ck_shop_cat_group.setChecked(cartBean.isGroupChecked());
            ck_shop_cat_group.setOnClickListener(new GroupClickListener(position, ck_shop_cat_group));
            tv_shop_name.setText(cartBean.getCompanyName());
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            CommodityAdapter adapter = new CommodityAdapter(mContext);
            recyclerView.setAdapter(adapter);
            adapter.setDataList(cartBean.getList());
            shopPosition = position;
        }
    }

    public class GroupClickListener implements View.OnClickListener {
        private int groupPosition;

        CheckBox group_cb;

        public GroupClickListener(int groupPosition, CheckBox group_cb) {
            this.groupPosition = groupPosition;
            this.group_cb = group_cb;
        }

        @Override
        public void onClick(View view) {
            if (((CheckBox) view).isChecked()) {
                setCheck(true);
            } else {
                setCheck(false);
            }
            notifyDataSetChanged();
        }

        //店铺按钮的修改状态的方法
        private void setCheck(boolean b) {
            CartBean.CartsBean cartBean = cartBeans.getCarts().get(groupPosition);
            cartBean.setGroupChecked(b);
            int num = 0;
            for (int i = 0; i < cartBeans.getCarts().size(); i++) {
                boolean allCheck = cartBeans.getCarts().get(i).isGroupChecked();
                if (!allCheck) {
                    num++;
                }
            }
            if (num == 0) {
                cb_check_all.setChecked(true);
            } else {
                cb_check_all.setChecked(false);
            }
            List<CartBean.CartsBean.ListBean> setMealList = cartBeans.getCarts().get(groupPosition).getList();
            for (CartBean.CartsBean.ListBean setMealListBean : setMealList) {
                setMealListBean.setChildChecked(b);
            }
        }
    }

    class CommodityAdapter extends ListBaseAdapter<CartBean.CartsBean.ListBean> {

        public CommodityAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_commodity_group;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            final CartBean.CartsBean.ListBean listBean = mDataList.get(position);
            final CheckBox cb_commodity = holder.getView(R.id.cb_commodity);
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_introduce = holder.getView(R.id.tv_commodity_introduce);
            TextView tv_commodity_active = holder.getView(R.id.tv_commodity_active);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            RelativeLayout rl_cart_item = holder.getView(R.id.rl_cart_item);
            TextView tv_xiajia = holder.getView(R.id.tv_xiajia);//下架的标识
//                tv_commodity_active.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            AmountView amountView = holder.getView(R.id.amountView);

            if (TextUtils.equals("0", listBean.getStatus())) {//0 下线 1 在售
                tv_xiajia.setVisibility(View.VISIBLE);
            } else {
                tv_xiajia.setVisibility(View.GONE);
            }
            if (!listBean.isUpdate()) {//说明是更新过规格的商品,不能买
                amountView.setVisibility(View.GONE);//GONE
                tv_commodity_active.setText("商品已更新");
            } else {
                amountView.setVisibility(View.VISIBLE);
                tv_commodity_active.setText(listBean.getSkuName());
            }

            amountView.setTextSize(16, 12);

            Glide.with(mContext).load(Config.BASE_URL + listBean.getImageUrl()).into(iv_commodity);
            amountView.setGoods_storage(999);
            amountView.setAmounts(listBean.getCount());
            tv_commodity_price.setText("￥" + listBean.getGoodsPrice());
            tv_commodity_introduce.setText(listBean.getGoodsName());
            cb_commodity.setChecked(listBean.isChildChecked());
            cb_commodity.setOnClickListener(new ChildCheckListener(shopPosition, position, cb_commodity));
            amountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
                @Override
                public void onAmountChange(View view, int amount) {
                    showProgress();
                    mPresenter.updateCartNum(listBean.getId(), amount);
                }
            });
            amountView.setOnTextViewOnClickListener(new AmountView.OnTextViewOnClickListener() {
                @Override
                public void onTextViewClick(View view) {
                    count = listBean.getCount();
                    showDialog(listBean.getId());
                }
            });

            rl_cart_item.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    id = listBean.getId();
                    deleteDialog(id);
                    return false;
                }
            });

            rl_cart_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Product product = new Product();
                    product.setGoodId(listBean.getGoodsId());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("product", product);
                    try {
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    public class ChildCheckListener implements View.OnClickListener {
        int groupPosition;
        int childPosition;
        CheckBox cb_child;

        public ChildCheckListener(int groupPosition, int childPosition, CheckBox cb_child) {
            this.groupPosition = groupPosition;
            this.childPosition = childPosition;
            this.cb_child = cb_child;
        }

        //商品选中按钮的改变店铺和全选的选中按钮的状态
        @Override
        public void onClick(View view) {
            if (((CheckBox) view).isChecked()) {
                cartBeans.getCarts().get(groupPosition).getList().get(childPosition).setChildChecked(true);
            } else {
                cartBeans.getCarts().get(groupPosition).getList().get(childPosition).setChildChecked(false);
            }
            setParentCheckFlag();
            int num = 0;
            for (int i = 0; i < cartBeans.getCarts().size(); i++) {
                boolean allCheck = cartBeans.getCarts().get(i).isGroupChecked();
                if (!allCheck) {
                    num++;
                }
            }
            if (num == 0) {
                cb_check_all.setChecked(true);
            } else {
                cb_check_all.setChecked(false);
            }
        }

        //修改店铺的选中状态
        private void setParentCheckFlag() {
            List<CartBean.CartsBean.ListBean> setMealList = cartBeans.getCarts().get(groupPosition).getList();
            for (int i = 0; i < setMealList.size(); i++) {
                if (!setMealList.get(i).isChildChecked()) {
                    cartBeans.getCarts().get(groupPosition).setGroupChecked(false);
                    notifyDataSetChanged();
                    return;
                }
                if (i == setMealList.size() - 1) {
                    cartBeans.getCarts().get(groupPosition).setGroupChecked(true);
                    notifyDataSetChanged();
                    return;
                }
            }
        }
    }


    private void setAll() {
        cb_check_all.setChecked(isCheckedAll());
    }

    private boolean isCheckedAll() {
        for (CartBean.CartsBean cartBean : cartBeans.getCarts()) {
            if (!cartBean.isGroupChecked()) {
                return false;
            }
        }
        return true;
    }

    private boolean isShopCommodityAll(int shopPosition) {
        for (CartBean.CartsBean.ListBean setMealListBean : cartBeans.getCarts().get(shopPosition).getList()) {
            if (!setMealListBean.isChildChecked()) {
                return false;
            }
        }
        return true;
    }

    private void notifyDataSetChanged() {
        String ids = null;
        StringBuffer buffer = new StringBuffer();
        int amount = 0;
        for (int i = 0; i < cartBeans.getCarts().size(); i++) {
            List<CartBean.CartsBean.ListBean> list = cartBeans.getCarts().get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).isChildChecked()) {
                    amount++;
                    ids = buffer.append(cartBeans.getCarts().get(i).getList().get(j).getId()).append(",").toString();
                }
            }
        }
        tv_total.setText("计算中...");
        if (!TextUtils.isEmpty(ids)) {
            mPresenter.calCarts(ids);
        } else {
            tv_total.setText("￥" + 0);
        }
        setAll();
        deleteAmount = amount;
        tv_commodity_amount.setText("去结算" + "(" + amount + ")");
        adapter.setDataList(cartBeans.getCarts());
    }

    public void setCheckSelectInterface(CheckSelectInterface checkSelectInterface) {
        this.checkSelectInterface = checkSelectInterface;
    }

    //修改购车数量的
    private void showDialog(final String cartId) {
        final AlertDialog dialog = new AlertDialog.Builder(getContext()).create();
        View view = View.inflate(getContext(), R.layout.item_commodity_num, null);
        final EditText et_commodity_num = view.findViewById(R.id.et_commodity_num);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        TextView tv_sure = view.findViewById(R.id.tv_sure);
        final TextView tv_commodity_num_move = view.findViewById(R.id.tv_commodity_num_move);
        final TextView tv_commodity_num_add = view.findViewById(R.id.tv_commodity_num_add);
        et_commodity_num.setText(count + "");
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(et_commodity_num.getText().toString())) {
                    count = Integer.parseInt(et_commodity_num.getText().toString());
                    if (count > 999) {
                        et_commodity_num.setText(999 + "");
                        count = 999;
                    }
                }
            }
        };
        et_commodity_num.addTextChangedListener(textWatcher);
        tv_commodity_num_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int amount = count;
                if (amount >= 999) {
                } else {
                    amount++;
                    et_commodity_num.setText(amount + "");
                }
            }
        });
        tv_commodity_num_move.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int amount = count;
                if (amount <= 1) {
                } else {
                    amount--;
                    et_commodity_num.setText(amount + "");
                }
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showProgress();
                mPresenter.updateCartNum(cartId, count);
                dialog.dismiss();
            }
        });
        dialog.setView(view);
        dialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    public void deleteDialog(final String ids) {
        alertDialog = new AlertDialog.Builder(getContext()).create();
        View view = View.inflate(getContext(), R.layout.item_delete_dialog, null);
        alertDialog.setView(view);
        alertDialog.show();
        TextView tv_delete_commodity_num = view.findViewById(R.id.tv_delete_commodity_num);
        TextView tv_ok = view.findViewById(R.id.tv_ok);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        if (deleteAmount > 1) {
            tv_delete_commodity_num.setText("确认删除" + deleteAmount + "件商品吗?");
        } else {
            tv_delete_commodity_num.setText("确认删除该商品吗?");
        }

        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgress();
                mPresenter.deleteByIds(ids);
                alertDialog.dismiss();
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
    }
}
